//
//  AccountAndSecurityVC.swift
//  UNHimalayas
//
//  Created by 王水平 on 2019/11/26.
//  Copyright © 2019 HachiTech. All rights reserved.
//

import UIKit

class AccountAndSecurityVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var tableView : UITableView!
    var dataArray = ["账号绑定","设置密码","最近登录设备","注销账户",]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.edgesForExtendedLayout = []
        self.navigationItem.title = "账号与安全"
        self.view.backgroundColor = RGBA(240, g: 240, b: 240, a: 1.0);

        initAction()
        initTabelView()
        initFooterButton()
    }
    
    func initAction() -> Void {
        
        let leftBtn = UIButton()
        leftBtn.setImage(UIImage(named: "navi_back"), for: UIControl.State.normal)
        leftBtn.setImage(UIImage(named: "navi_back"), for: UIControl.State.highlighted)
           
        leftBtn.addTarget(self, action: #selector(AccountAndSecurityVC.leftAction), for: UIControl.Event.touchUpInside)
        // button自适应大小
        leftBtn.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView:leftBtn)
    }
    
    func initTabelView() -> Void {
        
        self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0), style: .plain)
        self.tableView.backgroundColor = UIColor.clear
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.view.addSubview(self.tableView)
        
        self.tableView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(self.view)
        }
    }
    
    
    func initFooterButton() -> Void {
        
        let btn = UIButton(type: .system)
        btn.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kRealWidth(50))
        btn.backgroundColor = UIColor.clear
        btn.setTitle("常见问题", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: kRealFont(13))
//        btn.setTitleColor(UIColor.blue, for: .normal)
        btn.addTarget(self, action: #selector(clickedBtn(btn:)), for: .touchUpInside)
        self.tableView.tableFooterView = btn
    }
    
    // MARK: - 协议
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellName = "AccountAndSecurityViewCellName"
        let cell = AccountAndSecurityViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: cellName)
        
        cell.titleLabel.text = dataArray[indexPath.row]
        
        return cell;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return kRealWidth(50)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
    }

    // MARK: - 点击
    
    @objc private func leftAction() -> Void {
        
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc private func clickedBtn(btn : UIButton) {
        
        print("常见问题")
    }

}
